# -*- coding: utf-8 -*-
# @project: huawang-api20230914
# @file： CoponPage.py
# @Time：2023/8/31 16:35
# @Author：liaogan
from time import sleep
import allure
from common.commin.commin import input_text, click_element, get_element_text

from data.yk_names import Product_search, addCoponPage, PublicCopoun, mainPage
from po.jinhua.ht.jinhua_ht_main.jinhua_base import JHBase
from common.log.log import log


class AddCoponPage(JHBase):
    @allure.step('新增电子优惠券')
    def add_copon(self, CoponName, intRandom, desc='测试新增电子券'):
        try:
            log.info('电子券制作：新增电子券操作')
            click_element(self.driver, addCoponPage.el_add_button())  # 点击新增
            # click_element(self.driver, addCoponPage.el_public_copon())  # 点击通用券
            click_element(self.driver, addCoponPage.el_product_copon(), desc)  # 点商品券
            click_element(self.driver, addCoponPage.el_copon_2())  # 点击平台使用
            click_element(self.driver, addCoponPage.el_product_selectomania())  # 点击优惠券选择框
            sleep(2)
            click_element(self.driver, addCoponPage.el_second_proudct())  # 选择第二个商品
            click_element(self.driver, addCoponPage.el_verify_selector())  # 点击确认
            sleep(1)
            input_text(self.driver, addCoponPage.el_input_coponname(), CoponName)  # 输入优惠券名称
            input_text(self.driver, addCoponPage.el_input_coponsal_facevalue(), intRandom)  # 输入面值
            input_text(self.driver, addCoponPage.el_input_copon_minimumcharge(), 0)  # 最低消费
            input_text(self.driver, addCoponPage.el_input_coponsal_validity(), 30)  # 优惠券有限天数
            input_text(self.driver, addCoponPage.el_sort(), 1)  # 排序
            # click_element(self.driver, addCoponPage.el_status())  # 状态1开启  2关闭
            click_element(self.driver, addCoponPage.el_all_verify1())  # 点击确认
            sleep(2)
            log.info('电子券制作：新增电子优惠券成功')
        except Exception as e:
            log.error(f'新增电子品出现错误，异常为{e}')
            raise e

    @allure.step('发布电子优惠券')
    def IssueCoupons(self):
        log.info('电子券制作：发布优惠券操作')
        click_element(self.driver, PublicCopoun.el_PublicCopoun())  # 点击发布
        click_element(self.driver, PublicCopoun.el_StartTime())  # 选择开始时间
        click_element(self.driver, PublicCopoun.el_Selenow())  # 选择此时
        click_element(self.driver, PublicCopoun.el_EndTime())  # 选择结束时间
        click_element(self.driver, PublicCopoun.el_SeleEndTime())  # 选择9月23日
        input_text(self.driver, PublicCopoun.el_InputNum(), 10)  # 发布数量为10张
        click_element(self.driver, PublicCopoun.el_button())  # 点击确认
        sleep(2)
        log.info('电子券制作：发布优惠券成功')
        return AddCoponPage(self.driver)

    @allure.step('获取已发布第一个优惠券名称')
    def GetFirstCoupons(self):
        log.info('获取已发布的第一张优惠券')
        click_element(self.driver, mainPage.submenu_publish_coupon())  # 点击已经发布的优惠券
        click_element(self.driver, PublicCopoun.el_Refresh())  # 点击刷新
        sleep(2)
        resultName = get_element_text(self.driver, PublicCopoun.Get_title())  # 获取第一个优惠券名称
        sleep(1)
        return resultName

    @allure.step('获取新增优惠券tips提示文案')
    def get_yk_tips(self):
        try:
            tips = get_element_text(self.driver, addCoponPage.GetTipSuccess(), desc='优惠券0904')
            log.info(f'获取tips提示文案为：{tips}')
            return tips
        except Exception as e:
            log.error(f'获取新增优惠券tips提示文案出现错误，异常为{e}')
            raise e
